import { ArgumentMetadata, HttpException, HttpStatus, Injectable, PipeTransform } from '@nestjs/common';
import {plainToInstance} from 'class-transformer'
import {validate} from 'class-validator'
@Injectable()
export class ValidatorpiePipe implements PipeTransform {
  async transform(value: any, metadata: ArgumentMetadata) {
    // 实例化DTO
    const DTO = plainToInstance(metadata.metatype,value)
    const errors = await validate(DTO) as any
    if(errors.length > 0){
      throw new HttpException(errors[0].constraints,HttpStatus.BAD_REQUEST)
    }
    return value
  }
}
